<?php
/**
 * Created by PhpStorm.
 * User: 张良
 * Date: 2018/1/25
 * Time: 17:00
 */

namespace App\Http\Controllers\User;
use App\Http\Controllers\CommonController;
use Illuminate\Support\Facades\Input;
use Mockery\Exception;
use Request;
use DB;
use Redirect;

class forgetPasswordController extends CommonController
{
    public function forgetPasswordIndex(){
        return view('user/forgetPassword/index');
    }
    public function ajaxCheckTel(){
        $input=Request::all();
        $tel=$input['tel'];
        $res=object_to_array(DB::table('user')->where(array("phone"=>$tel))->first());
        if($res){
            $flag=0; //1 手机号存在
        }else{
            $flag=1; //0 手机号不存在
        }
        return json_encode($flag, JSON_UNESCAPED_UNICODE);
    }
    public function forgetPasswordReset(){
        $input=Request::all();
        $tel=$input['tel'];
//        dump($input);exit();
        if(empty($tel)){
            return back()->with('error','数据传输错误，请联系管理员');
        }else{
            return view('user/forgetPassword/reset')->with('data',$tel);
        }
    }
    public function ajaxCheckPwd(){
        $input=Request::all();
        $tel=$input['tel'];
        $pwd=md5($input['pwd']);
        $res=object_to_array(DB::table('user')->where(array("phone"=>$tel,"password"=>$pwd))->first());
        if($res){
            $flag=1; //1 新密码与旧密码相同
        }else{
            $flag=0; //0 新密码与旧密码不同
        }
        return json_encode($flag, JSON_UNESCAPED_UNICODE);
    }
    public function doReset(){
        $input=Request::all();
//        dump($input);exit();
        $data['password']=md5($input['new_pwd']);
        $res= DB::table('user')->where(array("phone"=>$input['hide_tel']))->update($data);
        if($res){
            return Redirect::to('/');
        }else{
            return Redirect::to('user/forgetPassword/forgetPasswordIndex');
        }
    }

}